package com.wjq.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjq.boot.entity.TAdmin;
import com.wjq.boot.service.TAdminService;
import com.wjq.boot.vo.ResultVo;
import lombok.Getter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.time.LocalDate;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangjiaquan
 * @since 2024-08-02
 */
@RestController
@RequestMapping("/tAdmin")
public class TAdminController {

    @Resource
    private TAdminService adminService;

    // 查询全部
    @GetMapping("/getall")
    public ResultVo<List<TAdmin>> getList(){
        return ResultVo.success(adminService.list(),"查询成功");
    }

    // 分页后内容
    @GetMapping("/list")
    public ResultVo<IPage<TAdmin>> getPage(Integer pageNo, Integer pageSize) {
        return ResultVo.success(adminService.page(new Page<>(pageNo,pageSize)),null);
    }

    // 添加管理员
    @PostMapping("/add")
    public ResultVo<Boolean> addAdmin(String name,
                                      String nickname,
                                      Boolean sex,
                                      LocalDate birthday,
                                      String phone,
                                      String email,
                                      String password,
                                      Integer state){
         return ResultVo.success(adminService.add(name, nickname, sex, birthday, phone, email, password, state),"添加成功");
    }

    @PostMapping("/update")
    public ResultVo<Boolean> uoDateAdmin(Integer id,
                                         String name,
                                         String nickname,
                                         Boolean sex,
                                         LocalDate birthday,
                                         String phone,
                                         String email,
                                         Integer state){
        return ResultVo.success(adminService.update(id,name,nickname,sex,birthday,phone,email,state),"修改成功");
    }

    // 用于测试String转LocalDate是否可用的接口
    @GetMapping("/test")
    public ResultVo<LocalDate> test(@RequestParam("data") LocalDate date){
        System.out.println(date);
        return ResultVo.success(date,null);
    }

    // 检查某个字段是否有重复的
    @GetMapping("/check")
    public ResultVo<Boolean> checkPhone(String column,String value,Integer id){
        return ResultVo.success(adminService.check(column,value,id),"不弹窗");
    }

    // 修改管理员状态
    @PostMapping("/fixState")
    public ResultVo<Boolean> fixState(Long id,Boolean state){
        return ResultVo.success(adminService.fixState(id,state),"修改成功");
    }

    // 获取一个管理员，用于表单回显
    @GetMapping("/getById")
    public ResultVo<TAdmin> getById(Long id){
        return ResultVo.success(adminService.getById(id),null);
    }

    @GetMapping("/find")
    public ResultVo<IPage<TAdmin>> getTargetList(Integer pageNo, Integer pageSize,String keyword){
        System.out.println(keyword);
        return ResultVo.success(adminService.getTargetList(pageNo,pageSize,keyword),null);
    }

}
